//
//  MEHomeViewController.swift
//  aj
//
//  Created by leeqb on 11/22/15.  
//  Copyright © 2015 powerlee. All rights reserved.
//

import UIKit

class MEHomeViewController: AJBaseTableViewController, UIAlertViewDelegate {
    @IBOutlet weak var headIcon: UIImageView!
    @IBOutlet weak var nickName: UILabel!
    @IBOutlet weak var sign: UILabel!
    @IBOutlet weak var wifiSwitch: UISwitch!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.tableView.zeroInsets()
        AJBaseController.addWhiteStatusBar(self)
        
        /*let url = AJUrlHelper.getAppServerUrl()
        print("userInfo is \(DataManager.userInfo()!["userId"])")
        let strParams: NSMutableDictionary = ["serviceName":"aj_user_Info", "callType":"002", "params":["userId":DataManager.userInfo()!["userId"] as! String]]
        //DataManager.userInfo()!["userId"] as! String
        let jsonData = try? NSJSONSerialization.dataWithJSONObject(strParams, options: [])
        let jsonStr = NSString(data: jsonData!, encoding: NSUTF8StringEncoding)
        let params: [NSObject : AnyObject] = ["jsonRequest":jsonStr!]
        
        // 发起请求
        CBNetworkHelper.shareInstance().post(url, parameters: params, success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) -> Void in
            // 请求成功
            //print(responseObject)
            if (responseObject["returnCode"] as? String == "000000"){
                let myData = responseObject["result"] as! [NSObject : AnyObject]
                //DataManager.setUserInfo(myData)
            }else{
                return
             }
           }, failed: { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
                // 请求失败
                print(error)
            }) { () -> Void in
                // 不管请求成功或失败都会走到这里来
        }*/
        
        headIcon.layer.masksToBounds = true
        headIcon.layer.cornerRadius = 30
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        // 显示头像
        headIcon.sd_setImageWithURL(NSURL(string: userPhoto), placeholderImage: g_placeImage)
        
        // 显示昵称
        if !userNickName.isEmpty {
            self.nickName.text = userNickName
        } else {
            self.nickName.text = "昵称未设置"
        }
        
        // 显示签名
        if !userSign.isEmpty {
            self.sign.text = userSign
        } else {
            self.sign.text = "个性签名未设置"
        }
    }
    
    override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section > 0 {
            return 20
        }
        
        return 0
    }
    
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        cell.zeroInsets()
    }
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: false)
        
        if indexPath.section == 0 { // 头像
            switch indexPath.row {
            case 0:
                let dataVC = LB("Me", vcName: "MyData") as! MEMyDataViewController
                self.navigationController?.pushViewController(dataVC, animated: true)
                break
            default:
                break
            }
        } else if indexPath.section == 1 { // 二维码下载/版本更新/关于爱家
            switch indexPath.row {
            case 0: // 二维码下载
                let codeVC = LB("Me", vcName: "code") as! UIViewController
                self.navigationController?.pushViewController(codeVC, animated: true)
                break
            case 1: // 关于爱家
                let aboutVC = LB("Me", vcName: "about") as! UIViewController
                self.navigationController?.pushViewController(aboutVC, animated: true)
                break
            default:
                break
            }
        } else if indexPath.section == 2 { // 清除缓存/只使用wifi上传照片
            switch indexPath.row {
            case 0: // 清除缓存
                let alertView = UIAlertView(title: "提示", message: "确定要清除缓存？", delegate: self, cancelButtonTitle: "取消", otherButtonTitles: "确定")
                alertView.show()
                break
            case 1: // 只使用wifi上传照片
                if wifiSwitch.on {
                    wifiSwitch.setOn(false, animated: true)
                } else {
                    wifiSwitch.setOn(true, animated: true)
                }
                break
            default:
                break
            }
        }
    }
    
    // MARK: - UIAlertViewDelegate
    func alertView(alertView: UIAlertView, didDismissWithButtonIndex buttonIndex: Int) {
        if buttonIndex == 1 {
            SDImageCache.sharedImageCache().cleanDisk()
            SDImageCache.sharedImageCache().clearDisk()
            SDImageCache.sharedImageCache().clearMemory()
            CBAlertView.shareInstance().showMsg("清除成功！")
        }
    }
}
